<?php

namespace app\model\main;

use linkphp\db\Db;
use linkphp\Application;
use linkphp\boot\Exception;

class Forum
{

    public function getForumList()
    {
        $page = isset($_GET['page']) ? Application::input('get.page') : '0';
        return Db::table('lp_forum')
            ->field('*')
            ->limit($page . ',10')
            ->select();
    }

    public function push($user)
    {
        Application::db()->beginTransaction();
        try{
            $data['title'] = Application::input('post.title');
            $data['c_id'] = 1;
            $data['u_id'] = 2;
            $data['post_time'] = date('Y-m-d H:i:s',time());
            $res['f_id'] = Db::table('lp_forum')
                ->insert($data);
            $res['content'] = Application::input('post.desc');
            $res['u_id'] = 2;
            $res['post_time'] = date('Y-m-d H:i:s',time());
            Db::table('lp_content')
                ->insert($res);
            Application::db()->commit();
            return true;
        } catch (Exception $e) {
            Application::db()->rollback();
            return $e->getMessage();
        }
    }

    public function getItem()
    {
//        $res['rep'] = Db::table('lp_forum_rep')
//            ->field('*')
//            ->where('forum_id = ' . Application::input('post.id'))
//            ->select();
        return Db::table('lp_forum a')
            ->field('*')
            ->join('left join lp_content b on a.id = b.f_id')
            ->where('a.id = ' . Application::input('post.id'))
            ->find();
    }

    public function reply($user)
    {
        $data['content'] = Application::input('post.content');
        $data['forum_id'] = Application::input('post.id');
        $data['u_id'] = $user['user_id'];
        $data['post_time'] = date('Y-m-d H:i:s',time());
        return Db::table('lp_forum_rep')
            ->insert($data);
    }

}